﻿/*
 * 由SharpDevelop创建。
 * 用户： LimitUser
 * 日期: 2013-1-16
 * 时间: 15:27
 * 
 * 要改变这种模板请点击 工具|选项|代码编写|编辑标准头文件
 */
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;
using DotNet.Common;
using Microsoft.Win32;

namespace AdminRun
{
	/// <summary>
	/// Description of MainForm.
	/// </summary>
	public partial class MainForm : FormDialog
	{
		XMLConfig<AppData> _appData;
		
		public MainForm()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			//
			// TODO: Add constructor code after the InitializeComponent() call.
			//
			_appData = new XMLConfig<AppData>();
			_appData.Loaded += appDataLoaded;
			_appData.Load(Application.StartupPath + @"\appData.xml");
		}
		
		void appDataLoaded(bool result,AppData data)
		{
			if(result)
			{
				textExeFilePath.Text = data.ExeFilepath;
				textUserName.Text = data.UserName;
				textPassword.Text = data.PassWord;
				textRunFormat.Text = data.RunFormat;
				textRunResult.Text = data.RunResult;
			}
			
			this.textExeFilePath.TextChanged += new System.EventHandler(this.UserInputTextChanged);
			this.textUserName.TextChanged += new System.EventHandler(this.UserInputTextChanged);
			this.textPassword.TextChanged += new System.EventHandler(this.UserInputTextChanged);
			this.textRunFormat.TextChanged += new System.EventHandler(this.UserInputTextChanged);
		}
		
		void BtnSelectExeFileClick(object sender, EventArgs e)
		{
			CommonApp.RunOpenFileDialog(this,"选择可执行程序的路径","可执行程序(*.exe;*.bat)|*.exe;*.bat",SelectExeFile);
		}
		
		bool SelectExeFile(string filePath)
		{
			textExeFilePath.Text = filePath;
			return true;
		}
		
		void UserInputTextChanged(object sender, EventArgs e)
		{
			GenResult();
		}
		
		void GenResult()
		{
			//<UserName> <PassWord> <WorkDir> "<TargetPath>"
			string result = textRunFormat.Text;
			result = result.Replace("<UserName>",textUserName.Text);
			result = result.Replace("<PassWord>",textPassword.Text);
			
			textRunResult.Text = result;
		}
		
		void BtnOKClick(object sender, EventArgs e)
		{
			if(textExeFilePath.Text.Length == 0 || textUserName.Text.Length == 0 || textPassword.Text.Length == 0)
			{
				CommonApp.RunErrorMessageBox(this,"信息输入不完全，请重新输入。");
				return;
			}
			
			_appData.Data.ExeFilepath = textExeFilePath.Text;
			_appData.Data.UserName = textUserName.Text;
			_appData.Data.PassWord = textPassword.Text;
			_appData.Data.RunFormat = textRunFormat.Text;
			_appData.Data.RunResult = textRunResult.Text;
			_appData.Save();
			
			CommonApp.RunInformationMessageBox(this,"输入成功");
		}
		
		void BtnCancelClick(object sender, EventArgs e)
		{
			this.Close();
		}
		
		void BtnRegClick(object sender, EventArgs e)
		{
			try
			{
				RegistryKey key;
				key = Registry.ClassesRoot.CreateSubKey(@"*\shell\OpenByAdminRun",RegistryKeyPermissionCheck.ReadWriteSubTree);
				key.SetValue(string.Empty,"用管理员权限打开(&A)",RegistryValueKind.String);
				key = Registry.ClassesRoot.CreateSubKey(@"*\shell\OpenByAdminRun\command",RegistryKeyPermissionCheck.ReadWriteSubTree);
				key.SetValue(string.Empty,string.Format("\"{0}\" \"%1\" %*",Application.ExecutablePath),RegistryValueKind.String);
				
				CommonApp.RunInformationMessageBox(this,"注册成功");
			}
			catch
			{
				CommonApp.RunErrorMessageBox(this,"注册失败，需要管理员权限!");
			}	
		}
	}
}
